
#include "playscenes.h"
#include<QDebug>
#include<QGraphicsItem>
#include<QGraphicsView>
#include <QVBoxLayout>
#include"character.h"
#include<QPropertyAnimation>
#include"rubbish.h"
#include<QGraphicsItemAnimation>
#include<QVariantAnimation>
#include<QTimeLine>
#include<QPainter>
#include<QLabel>
#include<QFont>
#include<ctime>
#include<QMediaPlayer>
#include<QSoundEffect>
#include<QUrl>
#include<QAudioOutput>

PlayScene::PlayScene(QWidget *parent)
    : QMainWindow{parent}
{
    srand(time(NULL));

    this->setWindowTitle("原神");
    this->setWindowIcon(QIcon("://resource/tubiao.png"));//设置窗口标题和图标

    playmusic=new QMediaPlayer(this);//设置本界面的背景音乐
    QAudioOutput* audiooutput=new QAudioOutput(this);
    audiooutput->setVolume(0.6);
    playmusic->setSource(QUrl("qrc:/resource/playmusic.mp3"));
    playmusic->setAudioOutput(audiooutput);
    playmusic->setLoops(-1);

    this->setFixedSize(1024,648);//设置窗口大小

    this->scene=new QGraphicsScene(this);//创建场景
    scene->setSceneRect(0,0,11000,648);//设置场景大小

    this->failedscene=new FailedScene;

    this->successscene=new SuccessScene;

    QGraphicsView*view=new QGraphicsView(scene);//设置scene对应的view，并设置视图属性
    view->setRenderHint(QPainter::Antialiasing);//启用抗锯齿
    view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);//当场景内容发生变化时，只更新视图中需要更新的部分，提高渲染效率
    view->setOptimizationFlag(QGraphicsView::DontAdjustForAntialiasing, true);//关闭抗锯齿调整优化，保留原始像素细节，提高渲染性能
    view->setOptimizationFlag(QGraphicsView::DontSavePainterState, true);//关闭绘画状态保存优化，避免因为过多的状态保存导致内存占用过大
    view->setCacheMode(QGraphicsView::CacheBackground);//缓存背景，减少重复渲染和绘制操作，提高性能
    view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);//禁用水平和垂直滚动条
    view->setAlignment(Qt::AlignCenter);//设置视图的对齐方式为居中
    view->centerOn(0,0);//设置初始视角中心为（0，0）点

    QPixmap pixground("://resource/ground.png");//加载背景图片

    for(int i=0;i<36;i++)
    {
        QPixmap pixbackground(":/resource/background.png");
        QGraphicsPixmapItem* background=new QGraphicsPixmapItem(pixbackground);
        background->setScale(0.57);
        background->setPos(pixbackground.width()*0.57*i,0);
        scene->addItem(background);
    }

    for(int i=0;i<220;i++)//加载地面图片
    {
        QGraphicsItem* ground=new QGraphicsPixmapItem(pixground);
        ground->setPos(i*50,600);
        ground->setScale(0.3);
        scene->addItem(ground);
    }

    Rubbish* rubbish1=new Rubbish(":/resource/rubbish.png",0.22);//设置地图里的垃圾桶
    scene->addItem(rubbish1->rubbish);
    rubbish1->rubbish->setPos(rubbish1->rubbish->pos()+QPoint(-200,0));
    this->rubbishArr.push_back(rubbish1);

    Rubbish* rubbish2=new Rubbish(":/resource/rubbish.png",0.22);
    scene->addItem(rubbish2->rubbish);
    rubbish2->rubbish->setPos(rubbish2->rubbish->pos()+QPoint(1000,0));
    this->rubbishArr.push_back(rubbish2);

    Rubbish* rubbish3=new Rubbish(":/resource/rubbish.png",0.22);
    scene->addItem(rubbish3->rubbish);
    rubbish3->rubbish->setPos(rubbish3->rubbish->pos()+QPoint(750,-200));
    this->rubbishArr.push_back(rubbish3);

    Rubbish* rubbish4x=new Rubbish(":/resource/rubbish.png",0.22);
    scene->addItem(rubbish4x->rubbish);
    rubbish4x->rubbish->setPos(rubbish4x->rubbish->pos()+QPoint(1500,-300));
    this->rubbishArr.push_back(rubbish4x);


    Rubbish* rubbish4=new Rubbish(":/resource/rubbish.png",0.22);
    scene->addItem(rubbish4->rubbish);
    rubbish4->rubbish->setPos(rubbish4->rubbish->pos()+QPoint(2000,0));
    this->rubbishArr.push_back(rubbish4);

    Rubbish* rubbish5x=new Rubbish(":/resource/rubbish.png",0.22);
    scene->addItem(rubbish5x->rubbish);
    rubbish5x->rubbish->setPos(rubbish5x->rubbish->pos()+QPoint(2500,0));
    this->rubbishArr.push_back(rubbish5x);

    Rubbish* rubbish5=new Rubbish(":/resource/rubbish.png",0.22);
    scene->addItem(rubbish5->rubbish);
    rubbish5->rubbish->setPos(rubbish5->rubbish->pos()+QPoint(3000,-200));
    this->rubbishArr.push_back(rubbish5);

    Rubbish* rubbish6x=new Rubbish(":/resource/rubbish.png",0.22);
    scene->addItem(rubbish6x->rubbish);
    rubbish6x->rubbish->setPos(rubbish6x->rubbish->pos()+QPoint(3700,-300));
    this->rubbishArr.push_back(rubbish6x);

    Rubbish* rubbish6=new Rubbish(":/resource/rubbish.png",0.22);
    scene->addItem(rubbish6->rubbish);
    rubbish6->rubbish->setPos(rubbish6->rubbish->pos()+QPoint(4500,0));
    this->rubbishArr.push_back(rubbish6);

    Rubbish* rubbish7x=new Rubbish(":/resource/rubbish.png",0.22);
    scene->addItem(rubbish7x->rubbish);
    rubbish7x->rubbish->setPos(rubbish7x->rubbish->pos()+QPoint(5500,0));
    this->rubbishArr.push_back(rubbish7x);

    Rubbish* rubbish7=new Rubbish(":/resource/rubbish.png",0.22);
    scene->addItem(rubbish7->rubbish);
    rubbish7->rubbish->setPos(rubbish7->rubbish->pos()+QPoint(7000,0));
    this->rubbishArr.push_back(rubbish7);

    Rubbish* rubbish8x=new Rubbish(":/resource/rubbish.png",0.22);
    scene->addItem(rubbish8x->rubbish);
    rubbish8x->rubbish->setPos(rubbish8x->rubbish->pos()+QPoint(8000,-300));
    this->rubbishArr.push_back(rubbish8x);

    Rubbish* rubbish8=new Rubbish(":/resource/rubbish.png",0.22);
    scene->addItem(rubbish8->rubbish);
    rubbish8->rubbish->setPos(rubbish8->rubbish->pos()+QPoint(9000,-200));
    this->rubbishArr.push_back(rubbish8);

    Rubbish* rubbish9x=new Rubbish(":/resource/rubbish.png",0.22);
    scene->addItem(rubbish9x->rubbish);
    rubbish9x->rubbish->setPos(rubbish9x->rubbish->pos()+QPoint(10000,-300));
    this->rubbishArr.push_back(rubbish9x);

    Rubbish* rubbish9=new Rubbish(":/resource/rubbish.png",0.22);
    scene->addItem(rubbish9->rubbish);
    rubbish9->rubbish->setPos(rubbish9->rubbish->pos()+QPoint(10900,0));
    this->rubbishArr.push_back(rubbish9);

    Rubbish* rubbish10=new Rubbish(":/resource/rubbish.png",0.22);
    scene->addItem(rubbish10->rubbish);
    rubbish10->rubbish->setPos(rubbish10->rubbish->pos()+QPoint(400,0));
    this->rubbishArr.push_back(rubbish10);

    //设置地图里的敌人
    Enemy* enemy771=new Enemy(":/resource/gongzi.png",0.4,":/resource/gongzia.png",":/resource/gongzimove1.png",":/resource/gongzimove2.png",":/resource/gongziattack.png",2,2,"","qrc:/resource/nilufail.mp3");
    scene->addItem(enemy771->enemy);
    enemy771->health=3000;
    enemy771->damage=80;
    enemy771->enemy->setPos(enemy771->enemy->pos()+QPoint(4000,20));
    this->EnemyArr.push_back(enemy771);

    Enemy* enemy772=new Enemy(":/resource/gongzi.png",0.4,":/resource/gongzia.png",":/resource/gongzimove1.png",":/resource/gongzimove2.png",":/resource/gongziattack.png",2,2,"","qrc:/resource/nilufail.mp3");
    scene->addItem(enemy772->enemy);
    enemy772->health=6000;
    enemy772->damage=80;
    enemy772->enemy->setPos(enemy772->enemy->pos()+QPoint(6000,20));
    this->EnemyArr.push_back(enemy772);

    Enemy* enemy773=new Enemy(":/resource/gongzi.png",0.4,":/resource/gongzia.png",":/resource/gongzimove1.png",":/resource/gongzimove2.png",":/resource/gongziattack.png",2,2,"","qrc:/resource/nilufail.mp3");
    scene->addItem(enemy773->enemy);
    enemy773->health=10000;
    enemy773->damage=120;
    enemy773->enemy->setPos(enemy773->enemy->pos()+QPoint(8000,20));
    this->EnemyArr.push_back(enemy773);

    Enemy* enemymc1=new Enemy(":/resource/nilu.png",0.4,":/resource/nilua.png",":/resource/nilumove1.png",":/resource/nilumove2.png",":/resource/nilumove1.png",2,1,"","qrc:/resource/nilufail.mp3");
    scene->addItem(enemymc1->enemy);
    enemymc1->health=1000;
    enemymc1->damage=80;
    enemymc1->enemy->setPos(enemymc1->enemy->pos()+QPoint(1700,20));
    this->EnemyArr.push_back(enemymc1);

    Enemy* enemymc2=new Enemy(":/resource/nilu.png",0.4,":/resource/nilua.png",":/resource/nilumove1.png",":/resource/nilumove2.png",":/resource/nilumove1.png",2,1,"","qrc:/resource/nilufail.mp3");
    scene->addItem(enemymc2->enemy);
    enemymc2->health=1000;
    enemymc2->damage=80;
    enemymc2->enemy->setPos(enemymc2->enemy->pos()+QPoint(2700,20));
    this->EnemyArr.push_back(enemymc2);

    Enemy* enemymc3=new Enemy(":/resource/nilu.png",0.4,":/resource/nilua.png",":/resource/nilumove1.png",":/resource/nilumove2.png",":/resource/nilumove1.png",2,1,"","qrc:/resource/nilufail.mp3");
    scene->addItem(enemymc3->enemy);
    enemymc3->health=1000;
    enemymc3->damage=80;
    enemymc3->enemy->setPos(enemymc3->enemy->pos()+QPoint(3700,20));
    this->EnemyArr.push_back(enemymc3);

    Enemy* enemymc4=new Enemy(":/resource/nilu.png",0.4,":/resource/nilua.png",":/resource/nilumove1.png",":/resource/nilumove2.png",":/resource/nilumove1.png",2,1,"","qrc:/resource/nilufail.mp3");
    scene->addItem(enemymc4->enemy);
    enemymc4->health=2000;
    enemymc4->damage=80;
    enemymc4->enemy->setPos(enemymc4->enemy->pos()+QPoint(4300,20));
    this->EnemyArr.push_back(enemymc4);

    Enemy* enemymc5=new Enemy(":/resource/nilu.png",0.4,":/resource/nilua.png",":/resource/nilumove1.png",":/resource/nilumove2.png",":/resource/nilumove1.png",2,1,"","qrc:/resource/nilufail.mp3");
    scene->addItem(enemymc5->enemy);
    enemymc5->health=2000;
    enemymc5->damage=80;
    enemymc5->enemy->setPos(enemymc5->enemy->pos()+QPoint(5100,20));
    this->EnemyArr.push_back(enemymc5);

    Enemy* enemymc6=new Enemy(":/resource/nilu.png",0.4,":/resource/nilua.png",":/resource/nilumove1.png",":/resource/nilumove2.png",":/resource/nilumove1.png",2,1,"","qrc:/resource/nilufail.mp3");
    scene->addItem(enemymc6->enemy);
    enemymc6->health=2000;
    enemymc6->damage=80;
    enemymc6->enemy->setPos(enemymc6->enemy->pos()+QPoint(5600,20));
    this->EnemyArr.push_back(enemymc6);

    Enemy* enemymc7=new Enemy(":/resource/nilu.png",0.4,":/resource/nilua.png",":/resource/nilumove1.png",":/resource/nilumove2.png",":/resource/nilumove1.png",2,1,"","qrc:/resource/nilufail.mp3");
    scene->addItem(enemymc7->enemy);
    enemymc7->health=2000;
    enemymc7->damage=80;
    enemymc7->enemy->setPos(enemymc7->enemy->pos()+QPoint(6200,20));
    this->EnemyArr.push_back(enemymc7);

    Enemy* enemymc8=new Enemy(":/resource/nilu.png",0.4,":/resource/nilua.png",":/resource/nilumove1.png",":/resource/nilumove2.png",":/resource/nilumove1.png",2,1,"","qrc:/resource/nilufail.mp3");
    scene->addItem(enemymc8->enemy);
    enemymc8->health=2000;
    enemymc8->damage=80;
    enemymc8->enemy->setPos(enemymc8->enemy->pos()+QPoint(6800,20));
    this->EnemyArr.push_back(enemymc8);

    Enemy* enemymc9=new Enemy(":/resource/nilu.png",0.4,":/resource/nilua.png",":/resource/nilumove1.png",":/resource/nilumove2.png",":/resource/nilumove1.png",2,1,"","qrc:/resource/nilufail.mp3");
    scene->addItem(enemymc9->enemy);
    enemymc9->health=2000;
    enemymc9->damage=120;
    enemymc9->enemy->setPos(enemymc9->enemy->pos()+QPoint(7200,20));
    this->EnemyArr.push_back(enemymc9);

    Enemy* enemymc10=new Enemy(":/resource/nilu.png",0.4,":/resource/nilua.png",":/resource/nilumove1.png",":/resource/nilumove2.png",":/resource/nilumove1.png",2,1,"","qrc:/resource/nilufail.mp3");
    scene->addItem(enemymc10->enemy);
    enemymc10->health=2000;
    enemymc10->damage=120;
    enemymc10->enemy->setPos(enemymc10->enemy->pos()+QPoint(7600,20));
    this->EnemyArr.push_back(enemymc10);

    Enemy* enemypvz1=new Enemy(":/resource/xiaolumove1.png",0.4,":/resource/xiaolua.png",":/resource/xiaolumove1.png",":/resource/xiaolumove2.png",":/resource/xiaoluattack.png",1,2,"","qrc:/resource/nilufail.mp3");
    scene->addItem(enemypvz1->enemy);
    enemypvz1->health=300;
    enemypvz1->enemy->setPos(enemypvz1->enemy->pos()+QPoint(800,20));
    this->EnemyArr.push_back(enemypvz1);

    Enemy* enemypvz2=new Enemy(":/resource/xiaolumove1.png",0.4,":/resource/xiaolua.png",":/resource/xiaolumove1.png",":/resource/xiaolumove2.png",":/resource/xiaoluattack.png",1,2,"","qrc:/resource/nilufail.mp3");
    scene->addItem(enemypvz2->enemy);
    enemypvz2->health=300;
    enemypvz2->enemy->setPos(enemypvz2->enemy->pos()+QPoint(1400,20));
    this->EnemyArr.push_back(enemypvz2);

    Enemy* enemypvz3=new Enemy(":/resource/xiaolumove1.png",0.4,":/resource/xiaolua.png",":/resource/xiaolumove1.png",":/resource/xiaolumove2.png",":/resource/xiaoluattack.png",1,2,"","qrc:/resource/nilufail.mp3");
    scene->addItem(enemypvz3->enemy);
    enemypvz3->health=300;
    enemypvz3->enemy->setPos(enemypvz3->enemy->pos()+QPoint(2000,20));
    this->EnemyArr.push_back(enemypvz3);

    Enemy* enemypvz4=new Enemy(":/resource/xiaolumove1.png",0.4,":/resource/xiaolua.png",":/resource/xiaolumove1.png",":/resource/xiaolumove2.png",":/resource/xiaoluattack.png",1,2,"","qrc:/resource/nilufail.mp3");
    scene->addItem(enemypvz4->enemy);
    enemypvz4->health=300;
    enemypvz4->enemy->setPos(enemypvz4->enemy->pos()+QPoint(2600,20));
    this->EnemyArr.push_back(enemypvz4);

    Enemy* enemypvz5=new Enemy(":/resource/xiaolumove1.png",0.4,":/resource/xiaolua.png",":/resource/xiaolumove1.png",":/resource/xiaolumove2.png",":/resource/xiaoluattack.png",1,2,"","qrc:/resource/nilufail.mp3");
    scene->addItem(enemypvz5->enemy);
    enemypvz5->health=300;
    enemypvz5->enemy->setPos(enemypvz5->enemy->pos()+QPoint(3200,20));
    this->EnemyArr.push_back(enemypvz5);

    Enemy* enemypvz6=new Enemy(":/resource/xiaolumove1.png",0.4,":/resource/xiaolua.png",":/resource/xiaolumove1.png",":/resource/xiaolumove2.png",":/resource/xiaoluattack.png",1,2,"","qrc:/resource/nilufail.mp3");
    scene->addItem(enemypvz6->enemy);
    enemypvz6->health=300;
    enemypvz6->enemy->setPos(enemypvz6->enemy->pos()+QPoint(2800,20));
    this->EnemyArr.push_back(enemypvz6);

    Enemy* enemypvz7=new Enemy(":/resource/xiaolumove1.png",0.4,":/resource/xiaolua.png",":/resource/xiaolumove1.png",":/resource/xiaolumove2.png",":/resource/xiaoluattack.png",1,2,"","qrc:/resource/nilufail.mp3");
    scene->addItem(enemypvz7->enemy);
    enemypvz7->health=600;
    enemypvz7->enemy->setPos(enemypvz7->enemy->pos()+QPoint(3800,20));
    this->EnemyArr.push_back(enemypvz7);

    Enemy* enemypvz8=new Enemy(":/resource/xiaolumove1.png",0.4,":/resource/xiaolua.png",":/resource/xiaolumove1.png",":/resource/xiaolumove2.png",":/resource/xiaoluattack.png",1,2,"","qrc:/resource/nilufail.mp3");
    scene->addItem(enemypvz8->enemy);
    enemypvz8->health=600;
    enemypvz8->enemy->setPos(enemypvz8->enemy->pos()+QPoint(4200,20));
    this->EnemyArr.push_back(enemypvz8);

    Enemy* enemypvz9=new Enemy(":/resource/xiaolumove1.png",0.4,":/resource/xiaolua.png",":/resource/xiaolumove1.png",":/resource/xiaolumove2.png",":/resource/xiaoluattack.png",1,2,"","qrc:/resource/nilufail.mp3");
    scene->addItem(enemypvz9->enemy);
    enemypvz9->health=600;
    enemypvz9->enemy->setPos(enemypvz9->enemy->pos()+QPoint(4600,20));
    this->EnemyArr.push_back(enemypvz9);

    Enemy* enemypvz10=new Enemy(":/resource/xiaolumove1.png",0.4,":/resource/xiaolua.png",":/resource/xiaolumove1.png",":/resource/xiaolumove2.png",":/resource/xiaoluattack.png",1,2,"","qrc:/resource/nilufail.mp3");
    scene->addItem(enemypvz10->enemy);
    enemypvz10->health=1000;
    enemypvz10->enemy->setPos(enemypvz10->enemy->pos()+QPoint(5200,20));
    this->EnemyArr.push_back(enemypvz10);

    Enemy* enemypvz11=new Enemy(":/resource/xiaolumove1.png",0.4,":/resource/xiaolua.png",":/resource/xiaolumove1.png",":/resource/xiaolumove2.png",":/resource/xiaoluattack.png",1,2,"","qrc:/resource/nilufail.mp3");
    scene->addItem(enemypvz11->enemy);
    enemypvz11->health=1000;
    enemypvz11->enemy->setPos(enemypvz11->enemy->pos()+QPoint(5700,20));
    this->EnemyArr.push_back(enemypvz11);

    Enemy* enemypvz12=new Enemy(":/resource/xiaolumove1.png",0.4,":/resource/xiaolua.png",":/resource/xiaolumove1.png",":/resource/xiaolumove2.png",":/resource/xiaoluattack.png",1,2,"","qrc:/resource/nilufail.mp3");
    scene->addItem(enemypvz12->enemy);
    enemypvz12->health=1000;
    enemypvz12->enemy->setPos(enemypvz12->enemy->pos()+QPoint(6300,20));
    this->EnemyArr.push_back(enemypvz12);

    Enemy* enemypvz13=new Enemy(":/resource/xiaolumove1.png",0.4,":/resource/xiaolua.png",":/resource/xiaolumove1.png",":/resource/xiaolumove2.png",":/resource/xiaoluattack.png",1,2,"","qrc:/resource/nilufail.mp3");
    scene->addItem(enemypvz13->enemy);
    enemypvz13->health=1000;
    enemypvz13->enemy->setPos(enemypvz13->enemy->pos()+QPoint(6600,20));
    this->EnemyArr.push_back(enemypvz13);

    Enemy* enemypvz14=new Enemy(":/resource/xiaolumove1.png",0.4,":/resource/xiaolua.png",":/resource/xiaolumove1.png",":/resource/xiaolumove2.png",":/resource/xiaoluattack.png",1,2,"","qrc:/resource/nilufail.mp3");
    scene->addItem(enemypvz14->enemy);
    enemypvz14->health=1000;
    enemypvz14->enemy->setPos(enemypvz14->enemy->pos()+QPoint(6800,20));
    this->EnemyArr.push_back(enemypvz14);

    Enemy* enemypvz15=new Enemy(":/resource/xiaolumove1.png",0.4,":/resource/xiaolua.png",":/resource/xiaolumove1.png",":/resource/xiaolumove2.png",":/resource/xiaoluattack.png",1,2,"","qrc:/resource/nilufail.mp3");
    scene->addItem(enemypvz15->enemy);
    enemypvz15->health=1000;
    enemypvz15->enemy->setPos(enemypvz15->enemy->pos()+QPoint(6900,20));
    this->EnemyArr.push_back(enemypvz15);

    Enemy* star=new Enemy("://resource/aether.png",0.32,":/resource/aethera.png",":/resource/aethermove1.png",":/resource/aethermove2.png",":/resource/aetherattack.png",3,2,"qrc:/resource/aetherbeattack.mp3","qrc:/resource/aetherfail.mp3",1,"qrc:/resource/aetherattack1.mp3","qrc:/resource/aetherattack2.mp3","qrc:/resource/aetherattack3.mp3");
    scene->addItem(star->enemy);
    star->enemy->setPos(star->enemy->pos()+QPoint(10500,-10));
    star->health=40000;
    star->damage=120;
    this->EnemyArr.push_back(star);

    this->lumine =new Character;//创建角色

    scene->addItem(lumine->pose);

    this->moveTimer=new QTimer;//创建各种定时器
    this->moveActTimer=new QTimer;
    this->jumpTimer=new QTimer;
    this->add77=new QTimer;
    this->addmc=new QTimer;
    this->addpvz=new QTimer;

    //移动定时器
    connect(this->moveTimer,&QTimer::timeout,[=](){
        for(int k=0;k<this->rubbishArr.size();k++)//碰撞检测
            {
            isMeet(this->lumine,this->rubbishArr[k]);
        }
        for(int k=0;k<this->EnemyArr.size();k++)//检测敌人是否注意到角色
        {
            if(this->lumine->pose->x()-EnemyArr[k]->enemy->x()<1000&&this->lumine->pose->x()-EnemyArr[k]->enemy->x()>-1000)
                emit EnemyArr[k]->notice();
        }
        if((this->lumine->pose->x()>=0||this->dx>0)&&this->lumine->pose->y()>=0&&(this->lumine->pose->x()<=(scene->width()-30)||this->dx<0))//移动
            this->lumine->pose->setPos(this->lumine->pose->x()+this->dx*this->lumine->moveAddition,this->lumine->pose->y());
        view->centerOn(this->lumine->pose->pos());//调整视角中心为角色所在位置
    });

    //移动动画效果定时器
    connect(this->moveActTimer,&QTimer::timeout,[=](){
        QString pos=QString("://resource/move%1.png").arg((this->lumine->moveflag));//加载图片并进行切换，以实现移动动画效果
        QPixmap temp(pos);
        if(this->lumine->dest==1)
            temp=temp.transformed(QTransform().scale(-1, 1));
        this->lumine->pose->setPixmap(temp);
        if(this->lumine->moveflag<5)
            this->lumine->moveflag++;
        else
        {this->lumine->moveflag=1;}
    });

    //跳跃定时器，用于实现跳跃时的场景更新
    connect(this->jumpTimer,&QTimer::timeout,[=](){
        scene->update();
    });

    //攻击定时器
    connect(this->lumine->attackTimer,&QTimer::timeout,[=](){
        view->centerOn(this->lumine->pose->pos());
        scene->update();
    });

    //敌人类的信号槽
    for(int i=0;i<this->EnemyArr.size();i++)
    {
        //敌人注意到角色后，开始移动
        connect(EnemyArr[i],&Enemy::notice,[=](){
            EnemyArr[i]->moveTimer->start(150);
            if(EnemyArr[i]->enemyflag==1)//敌人类型为1，则开启召唤小怪的定时器
                {
                this->add77->start(10000);
                this->addmc->start(5000);
                this->addpvz->start(1000);
            }
        });

        //敌人类的移动定时器
        connect(EnemyArr[i]->moveTimer,&QTimer::timeout,[=](){
            if(this->EnemyArr[i]->enemy->x()>lumine->pose->x())
            {
                this->EnemyArr[i]->dx=-1;
                this->EnemyArr[i]->dest=0;
            }
            else
            {
                this->EnemyArr[i]->dx=1;
                this->EnemyArr[i]->dest=1;
            }
            for(int k=0;k<this->rubbishArr.size();k++)
                {
                if(this->EnemyArr[i]->enemy->collidesWithItem(this->rubbishArr[k]->rubbish))
                {
                    if(this->EnemyArr[i]->enemy->x()>this->rubbishArr[k]->rubbish->x()&&this->EnemyArr[i]->dest==0)
                    this->EnemyArr[i]->dx=0;
                    else if(this->EnemyArr[i]->enemy->x()<this->rubbishArr[k]->rubbish->x()&&this->EnemyArr[i]->dest==1)
                        this->EnemyArr[i]->dx=0;
                }
            }
            EnemyArr[i]->move(this->lumine);
        });
        //enemy击杀角色后发送信号，实现场景切换
        connect(this->EnemyArr[i],&Enemy::failed,[=](){
            this->hide();
            playmusic->pause();
            this->failedscene->setGeometry(this->geometry());
            this->failedscene->show();
        });
    }

    connect(this,&PlayScene::speedup,[=](){
        this->lumine->moveAddition+=0.5;
    });

    connect(this,&PlayScene::jumpup,[=](){
        this->lumine->jumpAddition+=0.5;
    });

    //胜利场景切换
    connect(this,&PlayScene::success,[=](){
        this->hide();
        this->successscene->setGeometry(this->geometry());
        this->successscene->show();
    });

    //召唤小怪的信号槽
    connect(this->add77,&QTimer::timeout,[=](){
        Enemy* enemy77x=new Enemy(":/resource/gongzi.png",0.4,":/resource/gongzia.png",":/resource/gongzimove1.png",":/resource/gongzimove2.png",":/resource/gongziattack.png",2,2,"","qrc:/resource/nilufail.mp3");
        scene->addItem(enemy77x->enemy);
        enemy77x->health=5000;
        enemy77x->damage=50;
        enemy77x->enemy->setPos(enemy77x->enemy->pos()+QPoint(9000+rand()%2000,20));
        this->EnemyArr.push_back(enemy77x);
        enemy77x->moveTimer->start();
    });

    connect(this->addmc,&QTimer::timeout,[=](){
        Enemy* enemymcx=new Enemy(":/resource/nilu.png",0.4,":/resource/nilua.png",":/resource/nilumove1.png",":/resource/nilumove2.png",":/resource/nilumove1.png",2,1,"","qrc:/resource/nilufail.mp3");
        scene->addItem(enemymcx->enemy);
        enemymcx->health=2000;
        enemymcx->damage=60;
        enemymcx->enemy->setPos(enemymcx->enemy->pos()+QPoint(9000+rand()%2000,20));
        this->EnemyArr.push_back(enemymcx);
        enemymcx->moveTimer->start();
    });

    connect(this->addpvz,&QTimer::timeout,[=](){
        Enemy* enemypvzx=new Enemy(":/resource/xiaolumove1.png",0.4,":/resource/xiaolua.png",":/resource/xiaolumove1.png",":/resource/xiaolumove2.png",":/resource/xiaoluattack.png",1,2,"","qrc:/resource/nilufail.mp3");
        scene->addItem(enemypvzx->enemy);
        enemypvzx->health=600;
        enemypvzx->enemy->setPos(enemypvzx->enemy->pos()+QPoint(9000+rand()%2000,20));
        this->EnemyArr.push_back(enemypvzx);
        enemypvzx->moveTimer->start();
    });

    this->setCentralWidget(view);

}

void PlayScene::keyPressEvent(QKeyEvent *event)
{
    bool moveKeyPressed = false;
    if(!this->moveTimer->isActive()&&!this->moveActTimer->isActive()&&jumpflag==0&&!this->lumine->attackTimer->isActive())
    {this->dx=0;
    this->lumine->moveflag=1;

    if(event->key()==Qt::Key_A)//向左移动
    {dx=-4;
        this->lumine->dest=0;
        moveKeyPressed = true;
    }
    else if(event->key()==Qt::Key_D)//向右移动
    {    dx=4;
        this->lumine->dest=1;
        moveKeyPressed = true;
    }

    if(dx)//开启定时器
    {
        this->moveTimer->start(10);
        this->moveActTimer->start(100);
    }
    }

    if(event->key()==Qt::Key_Space&&jumpflag==0)//跳跃
    {
    this->jump();
    this->jumpTimer->start(30);
    QPixmap pixjump(":/resource/jumping.png");
    if(this->lumine->dest==0)
        pixjump=pixjump.transformed(QTransform().scale(-1, 1));
    this->lumine->pose->setPixmap(pixjump);
    this->jumpflag=1;
    QTimer::singleShot(400,this,[=](){
        this->moveActTimer->stop();
        this->fall();
        QPixmap pixfall(":/resource/falling.png");
        if(this->lumine->dest==0)
            pixfall=pixfall.transformed(QTransform().scale(-1, 1));
        this->lumine->pose->setPixmap(pixfall);
        QTimer::singleShot(500,this,[=](){
            this->jumpflag=0;
            QTimer::singleShot(1000,this,[=](){
                this->jumpTimer->stop();
            });
        });
    });
    }

    if(event->key()==Qt::Key_F)//获取道具
    {
    for(int i=0;i<this->rubbishArr.size();i++)
    {
        if(this->lumine->pose->collidesWithItem(this->rubbishArr[i]->rubbish)&&this->rubbishArr[i]->isChecked==0)
        {
            this->getLabel(this->rubbishArr[i]);
            this->rubbishArr[i]->isChecked=1;
        }
    }
    }

    if(moveKeyPressed)
    {
    event->accept();
    }
    else
    {
    event->ignore();
    }

    return QMainWindow::keyPressEvent(event);
}

void PlayScene::keyReleaseEvent(QKeyEvent *event)
{
    if(event->isAutoRepeat())
    return;
    if(this->moveTimer->isActive()&&(event->key()==Qt::Key_A||event->key()==Qt::Key_D))
    {
        this->moveTimer->stop();
        this->moveActTimer->stop();
        this->dx=0;
        if(this->jumpflag==0)
        this->lumine->moveflag=0;
        QPixmap pixStanding(":/resource/standing.png");
        if(this->lumine->dest==0)
            pixStanding=pixStanding.transformed(QTransform().scale(-1, 1));
        else
            pixStanding=pixStanding.transformed(QTransform().scale(1, 1));
        this->lumine->pose->setPixmap(pixStanding);
    }
    if(event->key()==Qt::Key_Space)
    {
        QTimer::singleShot(800,this,[=](){
            QPixmap pixStanding(":/resource/standing.png");
            if(this->lumine->dest==0)
                pixStanding=pixStanding.transformed(QTransform().scale(-1, 1));
            this->lumine->pose->setPixmap(pixStanding);
        });
    }
    return QMainWindow::keyReleaseEvent(event);
}

void PlayScene::isMeet(Character* lumine,Rubbish* rubbishx)//角色与rubbish类的碰撞判定
{
    if(lumine->pose->collidesWithItem(rubbishx->rubbish))
    {
        if((lumine->pose->x()<=rubbishx->rubbish->x()&&this->dx>0)||(lumine->pose->x()>=rubbishx->rubbish->x()&&this->dx<0))
            dx=0;
        lumine->meetflag=1;
        rubbishx->meetflag=1;
        if(lumine->pose->y()-lumine->height>rubbishx->rubbish->y())
            lumine->pose->setPos(lumine->pose->x(),lumine->pose->y()+rubbishx->height);
    }
    else
    {
        lumine->meetflag=0;
        rubbishx->meetflag=0;
    }
}

void PlayScene::jump()//实现跳跃的函数
{
    QGraphicsItemAnimation* jumpAnimation=new QGraphicsItemAnimation;
    jumpAnimation->setItem(this->lumine->pose);

    QTimeLine* timeline=new QTimeLine(400);
    jumpAnimation->setTimeLine(timeline);

    jumpAnimation->setPosAt(1,QPoint(this->lumine->pose->x()+dx*50,this->lumine->pose->y()-200*this->lumine->jumpAddition));

    timeline->setStartFrame(0);
    timeline->setEndFrame(5);
    timeline->setEasingCurve(QEasingCurve::OutQuad);

    timeline->start();
}

void PlayScene::fall()//实现下降的函数
{
    QGraphicsItemAnimation* fallAnimation=new QGraphicsItemAnimation;
    fallAnimation->setItem(this->lumine->pose);

    QTimeLine* timelinef=new QTimeLine(400);
    fallAnimation->setTimeLine(timelinef);

    fallAnimation->setPosAt(1,QPoint(this->lumine->pose->x()+dx*50,this->lumine->pose->y()+200*this->lumine->jumpAddition));

    timelinef->setStartFrame(0);
    timelinef->setEndFrame(5);
    timelinef->setEasingCurve(QEasingCurve::InQuad);

    timelinef->start();
}

void PlayScene::mousePressEvent(QMouseEvent* event )//攻击事件
{
    if(!this->lumine->attackTimer->isActive()&&!this->jumpflag)
    {
        if(this->moveTimer->isActive())
        {
            this->moveTimer->stop();
            this->moveActTimer->stop();
        }

        this->lumine->attack();

        for(int k=0;k<this->EnemyArr.size();k++)
        {
            this->isAttacked(this->lumine,this->EnemyArr[k]);
        }

        this->lumine->attackTimer->start(30);
    QTimer::singleShot(100,this,[=](){
        this->lumine->attackflag++;
            if(this->lumine->attackflag>5)
            {
                lumine->attackflag=1;
            }
        QTimer::singleShot(150,this,[=](){
                int tx=0;
                if(this->lumine->dest==1)
                    tx=1;
                else
                    tx=-1;

            QPixmap pixStanding(":/resource/standing.png");
            if(this->lumine->dest==0)
                pixStanding=pixStanding.transformed(QTransform().scale(-1, 1));
            else
                pixStanding=pixStanding.transformed(QTransform().scale(1, 1));
            this->lumine->width=pixStanding.width()*0.3;
            this->lumine->height=pixStanding.height()*0.3;
            this->lumine->pose->setPixmap(pixStanding);
            if(this->lumine->attackflag==2)
                this->lumine->pose->setPos(this->lumine->pose->x()+5*tx,this->lumine->pose->y()-24);
            else if(this->lumine->attackflag==3)
                this->lumine->pose->setPos(this->lumine->pose->x()+5*tx,this->lumine->pose->y()+70);
            else if(this->lumine->attackflag==4)
                this->lumine->pose->setPos(this->lumine->pose->x()+5*tx,this->lumine->pose->y()+65);
            else if(this->lumine->attackflag==5)
                this->lumine->pose->setPos(this->lumine->pose->x()+50*tx,this->lumine->pose->y()-30);
            else if(this->lumine->attackflag==1)
                this->lumine->pose->setPos(this->lumine->pose->x()+40*tx,this->lumine->pose->y()-30);
            this->lumine->attackTimer->stop();
        });

        });
    }
    return QMainWindow::mousePressEvent(event);
}

void PlayScene::mouseReleaseEvent(QMouseEvent* event)
{
    this->lumine->pose->setPos(this->lumine->pose->x(),460);

    return QMainWindow::mouseReleaseEvent(event);
}

void PlayScene::isAttacked(Character* ,Enemy* enemyx)//敌人受击事件
{
    if(this->lumine->pose->collidesWithItem(enemyx->enemy))
    {
    int tx=0;
    if(this->lumine->dest==1)
            tx=1;
    else
            tx=-1;
    if(enemyx->beattackUrl!=" "&&enemyx->health>=0)
    {
            enemyx->beattacked->play();
            enemyx->beattack->play();
    }

    enemyx->health-=this->lumine->damage;
    if(enemyx->aPixPath!=" ")
    {
            enemyx->enemy->setPixmap(enemyx->aPixPath);
            QTimer::singleShot(300,this,[=](){
                if(enemyx->health>20)
                enemyx->enemy->setPixmap(enemyx->oPixPath);
            });
    }
    enemyx->enemy->setPos(enemyx->enemy->pos()+QPoint(40*tx,0));
    if(enemyx->health<=0&&enemyx->isAlive==1)
    {
            enemyx->isAlive=0;
            enemyx->fail->play();
            this->lumine->health+=50;
            this->lumine->damage+=2;
        this->scene->removeItem(enemyx->enemy);
//        delete enemyx;
        if(enemyx->enemyflag==1)
        {
            QTimer::singleShot(2000,this,[=](){
                emit this->success();
            });
        }
    }
    }
}

void PlayScene::getLabel(Rubbish* rubbish)//获取道具事件
{
    rubbish->getlabel->play();

    int x=rand()%100+1;
    QPixmap pix;

    if(x<=5&&x>=0)
    {
    pix.load(":/resource/jiuchan.png");
    this->lumine->health+=500;
    this->lumine->damage+=50;
    }

    else if(x<=10&&x>5)
    {
    pix.load(":/resource/wufengjian.jpg");
    this->lumine->damage+=80;
    }

    else if(x<=20&&x>10)
    {
    pix.load(":/resource/rubbishdignity.png");
    this->lumine->health+=500;
    }

    else if(x<=35&&x>20)
    {
    pix.load(":/resource/normalrubbish.png");
    this->lumine->health+=300;
    this->lumine->damage+=10;
    }

    else if(x<=50&&x>35)
    {
    pix.load(":/resource/goodrubbish.png");
    this->lumine->health-=100;
    this->lumine->damage+=50;
    }

    else if(x<=60&&x>50)
    {
    pix.load(":/resource/good.png");
    this->lumine->health+=500;
    }

    else if(x<=65&&x>60)
    {
    pix.load(":/resource/wuqie.jpg");
    this->lumine->damage+=100;
    }

    else if(x<=70&&x>65)
    {
    pix.load(":/resource/monv.png");
    this->lumine->health-=200;
    this->lumine->damage+=150;
    }

    else if(x<=75&&x>70)
    {
    pix.load(":/resource/dongji.png");
    this->lumine->damage+=75;
    if(this->lumine->health<=1000)
        this->lumine->damage+=50;
    this->lumine->health+=200;
    }

    else if(x<=85&&x>75)
    {
    pix.load(":/resource/mola.jpg");
    emit this->jumpup();
    this->lumine->damage+=100;
    this->lumine->health-=100;
    }

    else if(x<=95&&x>85)
    {
    pix.load(":/resource/xiansuping.jpg");
    emit this->speedup();
    this->lumine->health+=200;
    }

    else if(x<=98&&x>95)
    {
    pix.load("://resource/xiangyu.png");
    this->lumine->health+=1000;
    this->lumine->damage+=100;
    }

    else if(x<=100&&x>98)
    {
    pix.load(":/resource/yuanshi.jpg");
    if(x==100)
        this->lumine->health=1;
    for(int k=0;k<3;k++)
    {
        QTimer::singleShot(2000,this,[=](){
            rubbish->isChecked=0;
            this->getLabel(rubbish);
        });
    }
    }

    QGraphicsPixmapItem* item=new QGraphicsPixmapItem(pix);
    this->scene->addItem(item);
    item->setPos(this->lumine->pose->x()+100,300);

    qDebug()<<x;
    QTimer::singleShot(1000,this,[=](){
        this->scene->removeItem(item);
        delete item;
    });
}


